package com.guan.poi.core;

import com.guan.poi.core.face.Column;
import com.guan.poi.core.face.Titleable;
import com.guan.poi.util.PoiUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
@Accessors(chain = true)
public class PoiColumnItems<T, V> extends Node implements Titleable, Column<T> {
	private int dataIndex = 1;
	protected ItemFunction<V, String[]> titles;
	protected ItemFunction<T, Collection<V>> itemFunction;

	/** 标题样式 */
	private PoiStyle titleStyle;

	/**
	 * 无标题列
	 * @param itemFunction 获取值的Function, 空去默认, 无默认值为""
	 */
	public PoiColumnItems(ItemFunction<T, Collection<V>> itemFunction) {
		this.titles = PoiUtil.emptyTitle();
		this.itemFunction = itemFunction;
	}

	public PoiColumnItems(ItemFunction<T, Collection<V>> itemFunction, String... title) {
		this.titles = t1 -> title;
		this.itemFunction = itemFunction;
	}

	/**
	 * @param itemFunction 获取值的Function, 空去默认, 无默认值为""
	 * @param titles 标题,可以返回多行,每个是一行
	 */
	public PoiColumnItems(ItemFunction<T, Collection<V>> itemFunction, ItemFunction<V, String[]> titles) {
		this.titles = titles;
		this.itemFunction = itemFunction;
	}

	/** Column属性的全部定义 */
	private List<Column<V>> columns = new ArrayList<>();


	public  <V2> PoiColumnItems<V, V2> addColumns(ItemFunction<V, Collection<V2>> valueFunction, ItemFunction<V2, String[]> titles) {
		return addColumns(new PoiColumnItems<>(valueFunction, titles));
	}

	public <V2> PoiColumn<V> addColumn(ItemFunction<V, V2> valueFunction, ItemFunction<V, String[]> titles) {
		return addColumn(new PoiColumn<>(valueFunction, titles));
	}

	public <V2> PoiColumn<V> addColumn(ItemFunction<V, V2> valueFunction, String... titles) {
		return addColumn(new PoiColumn<>(valueFunction, item -> titles));
	}

	public <V2> PoiColumnItems<V, V2> addColumns(PoiColumnItems<V, V2> poiColumns) {
		poiColumns.parent(this);
		poiColumns.setDataIndex(dataIndex);
		columns.add(poiColumns);
		return poiColumns;
	}

	public PoiColumn<V> addColumn(PoiColumn<V> column) {
		column.parent(this);
		column.setDataIndex(dataIndex);
		columns.add(column);
		return column;
	}
}
